// components/cart-bar/index.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showCartModal:{
      type:Boolean,
      value:false
    }, 
    list:{
      type:Array,
      value:[]
    }
  },
  observers:{
    'list' :function(newList){
      this.calculateTotalPrice() 
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    showCartModal:false,
    showCartGoodsModal:true,
    totalPrice:0,
    list:[]
  },
  
  create(){
    // this.data.list=wx.getStorageSync('cartList')
    console.log("cart 组件="+this.data.list)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //计算总价格
    calculateTotalPrice() {
      let totalPrice = 0.00;
      this.data.list.forEach(good=>{
        console.log("fff"+good.spuSellPrice*good.number)
        totalPrice+=good.spuSellPrice*good.number
      }) 
      totalPrice = totalPrice.toFixed(2);
      this.setData({
        totalPrice:totalPrice
      })
    },
    showCartGoods:function(){
      
      this.setData({
        showCartGoodsModal:!this.data.showCartGoodsModal
      })
      console.log("showCartGoods")
    },
    toPay(){
      console.log("topay",app.globalData.userToken)
      if (!app.globalData.userToken) {
        console.log("9999")
        wx.switchTab({
          url: '/pages/me/me'
        })
        app.showMsg("请先登录")
        return
      }
      wx.navigateTo({
        url: '/pages/pay/pay'
      })
    },
    clearCart(){
      console.log("clearCart")
      this.triggerEvent('clearCartEvent', { }, { bubbles: true, composed: true });
    }
  }
})
